package oa.dao.impl;

import java.util.List;

import oa.base.BaseDao;
import oa.dao.UserDao;
import oa.entity.User;
import oa.exception.DaoException;

public class UserDaoImpl extends BaseDao<User> implements UserDao {

	/**
	 * 员工登陆
	 */
	@Override
	public User userLogin(String name, String pwd) {
		List<User> list = null;
		try {
			list = executeQuery("SELECT * FROM user WHERE login_name=? and login_pwd = ?", name, pwd);
			if (list != null && list.size() != 0) {
				return list.get(0);
			}
		} catch (DaoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 添加员工
	 */
	@Override
	public boolean addUser(User user) throws DaoException {
		if ("经理".equals(user.getUserPosition())) {
			return executeUpdate(
					"INSERT INTO `oa`.`user` (`user_name`, `login_name`, `login_pwd`, `user_sex`, `user_phone`, `user_email`, `user_address`, `user_identitycard`, `user_familyphone`, `user_diploma`, `dept_id`, `user_position`,  `user_entrytime`,`company_id`, `user_competence`) VALUES ( ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,  ?,1)",
					user.getUserName(), user.getLoginName(), user.getLoginPwd(), user.getUserSex(), user.getUserPhone(),
					user.getUserEmail(), user.getUserAddress(), user.getUserIdentitycard(), user.getUserFamilyphone(),
					user.getUserDiploma(), user.getDeptId(), user.getUserPosition(), user.getUserEntrytime(),
					user.getCompanyId()) > 0;

		} else {

			return executeUpdate(
					"INSERT INTO `oa`.`user` (`user_name`, `login_name`, `login_pwd`, `user_sex`, `user_phone`, `user_email`, `user_address`, `user_identitycard`, `user_familyphone`, `user_diploma`, `dept_id`, `user_position`,  `user_entrytime`,`company_id`) VALUES ( ?, ?, ?,  ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)",
					user.getUserName(), user.getLoginName(), user.getLoginPwd(), user.getUserSex(), user.getUserPhone(),
					user.getUserEmail(), user.getUserAddress(), user.getUserIdentitycard(), user.getUserFamilyphone(),
					user.getUserDiploma(), user.getDeptId(), user.getUserPosition(), user.getUserEntrytime(),
					user.getCompanyId()) > 0;

		}
	}

	/**
	 * 查询全部员工
	 */
	@Override
	public List<User> selectAllUser(User user, Integer pageNo, Integer row) throws DaoException {
		if (user == null && pageNo == 0 && row == 0) {
			return executeQuery("SELECT * FROM user");
		} else if (user == null) {
			return executeQuery("SELECT * FROM user LIMIT ?,?", pageNo, row);
		} else if (user.getCompanyId() == 0) {
			return executeQuery("SELECT * FROM `user` WHERE `user`.dept_id =?   LIMIT ?,?", user.getDeptId(),
					(pageNo - 1) * row, row);
		} else if (user.getCompanyId() != 0 && user.getDeptId() != 0) {
			return executeQuery("SELECT * FROM `user` WHERE `user`.dept_id =? AND `user`.company_id =?  LIMIT ?,?",
					user.getDeptId(), user.getCompanyId(), (pageNo - 1) * row, row);

		}else if(user.getCompanyId() != 0) {
			return executeQuery("SELECT * FROM `user` WHERE  `user`.company_id =?  LIMIT ?,?",
					 user.getCompanyId(), (pageNo - 1) * row, row);

		}else{
			return executeQuery("SELECT * FROM `user` LIMIT ?,?", (pageNo - 1) * row, row);
		}		

	}

	/**
	 * 根据条件查询
	 */
	@Override
	public List<User> selectUser(int userId, String userName, int deptId, int... companyId) throws DaoException {
		String sql = null;
		Object obj = null;
		if (userId != 0) {
			sql = "SELECT * FROM `user` WHERE `user_id` = ? ";
			obj = userId;
		} else if (userName != null) {
			sql = "SELECT * FROM `user` WHERE `user_name` = ?";
			obj = userName;
		} else if (companyId.length != 0) {
			sql = "SELECT * FROM `user` WHERE `company_id` = ?";
			obj = companyId[0];
		} else {

			sql = "SELECT * FROM `user`";
			obj = deptId;
		}
		return executeQuery(sql, obj);
	}

	@Override
	public boolean updateUser(User user) throws DaoException {
		
			return executeUpdate(
					"UPDATE `oa`.`user` SET `user_name` = ? , `login_name` = ? , `login_pwd` = ? , `user_sex` =? , `user_phone` = ? , `user_email` = ? , `user_address` = ? , `user_identitycard` = ? , `user_familyphone` = ? ,`user_diploma` = ? , `dept_id` = ? , `user_position` = ? , `user_entrytime` = ? ,`company_id` = ?  WHERE `user_id` = ?",
					user.getUserName(), user.getLoginName(), user.getLoginPwd(), user.getUserSex(), user.getUserPhone(),
					user.getUserEmail(), user.getUserAddress(), user.getUserIdentitycard(), user.getUserFamilyphone(),
					user.getUserDiploma(), user.getDeptId(), user.getUserPosition(), user.getUserEntrytime(),
					user.getCompanyId(), user.getUserId()) > 0;
		

	}

	@Override
	public boolean deleteUser(int userId) throws DaoException {
		return executeUpdate("DELETE FROM user WHERE user_id = ?", userId) > 0;
	}

	@Override
	public int selectAllUserCount(int deptid,int companyid) throws DaoException {
		if (deptid != 0) {
			return executeIntQuery("SELECT COUNT(*) FROM `user` WHERE `dept_id` = ?",deptid);
		} else  if(companyid!=0){
			return executeIntQuery("SELECT COUNT(*) FROM `user` WHERE `company_id`=?", companyid);
		}else {
			return executeIntQuery("SELECT COUNT(*) FROM `user`");
		}
		
	}

	@Override
	public boolean updateUserFuZe(User user) throws DaoException {
		return executeUpdate("UPDATE `oa`.`user` SET `user_competence` =?,`user_position` = ? WHERE `user_id` = ?;",
		 user.getUserCompetence(),user.getUserPosition(), user.getUserId()) > 0;

	}

}
